//
//  UserData.m
//  DeyiLife
//
//  Created by zxin on 15/7/10.
//  Copyright (c) 2015年 Deyi. All rights reserved.
//

#import "UserData.h"
#import "MMLauchImage.h"
#import <HyphenateLite/HyphenateLite.h>

#define UD [NSUserDefaults standardUserDefaults]
#define UDSEARCHLIST @"udsearchlist"
#define UDTOKEN @"udtoken"
#define UDUID @"uduid"
#define UDNAME @"udname"
#define UDPHONE @"uduphone"
#define UDHEADURL @"udheadurl"
#define UDEASEMOBNAME @"udeasemobname"
#define UDEASEMOBPASS @"udeasemobpass"
#define UDLAUNCHIMAGESHOW @"udlauchimageshow"
#define UDLAUNCHIMAGEDOWN @"udlauchimagedown"
#define UDSTARTTIME @"udstarttime"
#define UDADSHOWTIME @"udadshowtime"
#define UDSHOWLAUNCHTIME @"udshowlaunchtime"
#define UDVERSION @"udversion"

@implementation UserData

#pragma mark - search list
+ (NSMutableArray *)searchKeyList {
    NSMutableArray *array = [[UD objectForKey:UDSEARCHLIST] mutableCopy];;
    if (!array) {
        array = [[NSMutableArray alloc] init];
    }
    return array;
}

+ (void)setSearchKeyList:(NSMutableArray *)list {
    [UD setObject:list forKey:UDSEARCHLIST];
    [UD synchronize];
}

#pragma mark - user
+ (void)login:(NSDictionary *)dict {
    [self logout];
    [UserData setUserid:[dict valueForKey:@"user_id"]];
    [UserData setUsertoken:[dict valueForKey:@"token"]];
    [UserData setUsername:[dict valueForKey:@"nick_name"]];
    [UserData setUserphone:[dict valueForKey:@"user_mobile_no"]];
    [UserData setUserheadurl:[dict valueForKey:@"head_url"]];
    [UserData setMob_username:[dict valueForKey:@"easemob_user_name"]];
    [UserData setMob_userpass:[dict valueForKey:@"easemob_user_pass"]];
    BOOL isjoin = [[dict valueForKey:@"isJoinTribe"] boolValue];
    [UD setBool:isjoin forKey:@"udisjointribe"];
    [UD synchronize];
    
    NSString *easemob_name = [dict valueForKey:@"easemob_user_name"];
    NSString *easemob_pass = [dict valueForKey:@"easemob_user_pass"];
    DYLog(@"name:%@ \n pass:%@", [UserData easemob_username], [UserData easemob_userpass]);
//    EMError *error = [[EMClient sharedClient] loginWithUsername:easemob_name password:easemob_pass];
//    if (!error) {
//        [[EMClient sharedClient].options setIsAutoLogin:YES];
//    }
    
    [[EMClient sharedClient] loginWithUsername:[UserData easemob_username] password:[UserData easemob_userpass]completion:^(NSString *aUsername, EMError *aError) {
                                        if (!aError) {
                                            [[EMClient sharedClient].options setIsAutoLogin:YES];
                                            NSLog(@"登陆成功");
                                        } else {
                                            NSLog(@"登陆失败");
                                        }
                                    }];
}

+ (void)logout {
    [UserData setUserid:@"0"];
    [UserData setUsertoken:@""];
    [UserData setUserheadurl:@""];
    [UserData setMob_username:@""];
    [UserData setMob_userpass:@""];
    [UD setBool:NO forKey:@"udisjointribe"];
    [UD synchronize];
    
    EMError *error = [[EMClient sharedClient] logout:YES];
    if (!error) {
        NSLog(@"退出成功");
    }
}

+ (BOOL)isLogin {
    if ([[UserData userid] isEqualToString:@"0"])
        return NO;
    return YES;
}

+ (BOOL)isJoinTribe {
    if (![UD boolForKey:@"udisjointribe"]) {
        return NO;
    }
    return YES;
}

+ (NSString *)userid {
    if (![UD stringForKey:UDUID]) {
        return @"0";
    }
    return [UD stringForKey:UDUID];
}

+ (NSString *)username {
    if (![UD stringForKey:UDNAME]) {
        return @"";
    }
    return [UD stringForKey:UDNAME];
}

+ (NSString *)usertoken {
    if (![UD stringForKey:UDTOKEN]) {
        return @"";
    }
    return [UD stringForKey:UDTOKEN];
}

+ (NSString *)userphone {
    if (![UD stringForKey:UDPHONE]) {
        return @"";
    }
    return [UD stringForKey:UDPHONE];
}

+ (NSString *)userheadurl {
    if (![UD stringForKey:UDHEADURL]) {
        return @"";
    }
    return [UD stringForKey:UDHEADURL];
}

+ (NSString *)easemob_username {
    if (![UD stringForKey:UDEASEMOBNAME]) {
        return @"";
    }
    return [UD stringForKey:UDEASEMOBNAME];
}

+ (NSString *)easemob_userpass {
    if (![UD stringForKey:UDEASEMOBPASS]) {
        return @"";
    }
    return [UD stringForKey:UDEASEMOBPASS];
}

+ (void)setUserid:(NSString *)userid {
    [UD setObject:userid forKey:UDUID];
}

+ (void)setUsername:(NSString *)username {
    [UD setObject:username forKey:UDNAME];
}

+ (void)setUsertoken:(NSString *)usertoken {
    [UD setObject:usertoken forKey:UDTOKEN];
}

+ (void)setUserphone:(NSString *)userphone {
    [UD setObject:userphone forKey:UDPHONE];
}

+ (void)setUserheadurl:(NSString *)userheadurl {
    [UD setObject:userheadurl forKey:UDHEADURL];
    [UD synchronize];
}

+ (void)setMob_username:(NSString *)username {
    [UD setObject:username forKey:UDEASEMOBNAME];
}

+ (void)setMob_userpass:(NSString *)userpass {
   [UD setObject:userpass forKey:UDEASEMOBPASS];
}

+ (BOOL)shouldShowLaunchImage {
    if (![UD boolForKey:UDLAUNCHIMAGESHOW])
        return NO;
    //    NSString *cityid = [self cityid];
    //    NSString *preid = [self preCityid];
    //    if (![[self cityid] isEqualToString:[self preCityid]]) {
    //        NSString *cityid = [self cityid];
    //        NSString *precityid = [self preCityid];
    //        return NO;
    //    }
    MMLauchImage *launchInfo = [[TMCache sharedCache] objectForKey:MMLAUCH_IMAGE];
//    if (launchInfo) {
//        long long start = [launchInfo.stime longLongValue];
//        long long end = [launchInfo.etime longLongValue];
//        long long now = [[TimeHelper currentTimeStamp] longLongValue];
//        if (now < start || now > end) {
//            return NO;
//        }
//    }
    return [UD boolForKey:UDLAUNCHIMAGESHOW];
}

+ (void)setShouldShowLauchImage:(BOOL)should {
    [UD setBool:should forKey:UDLAUNCHIMAGESHOW];
    [UD synchronize];
}

+ (BOOL)launchImageHasDownload {
    if (![UD boolForKey:UDLAUNCHIMAGEDOWN])
        return NO;
    return [UD boolForKey:UDLAUNCHIMAGEDOWN];
}

+ (void)setLauchImageHasDownload:(BOOL)hasDown {
    [UD setBool:hasDown forKey:UDLAUNCHIMAGEDOWN];
    [UD synchronize];
}

+ (void)setShowLaunchTime:(NSString *)timestamp {
    [UD setObject:timestamp forKey:UDSHOWLAUNCHTIME];
    [UD synchronize];
}

+ (NSString *)showLaunchTime {
    if (![UD stringForKey:UDSHOWLAUNCHTIME]) {
        return @"";
    }
    return [UD stringForKey:UDSHOWLAUNCHTIME];
}

+ (void)setAdShowTimeStamp:(NSString *)timestamp {
    [UD setObject:timestamp forKey:UDADSHOWTIME];
    [UD synchronize];
}

+ (NSString *)adShowTimeStamp {
    if (![UD stringForKey:UDADSHOWTIME]) {
        return @"0";
    }
    return [UD stringForKey:UDADSHOWTIME];
}

+ (NSString *)preVersion {
    if (![UD stringForKey:UDVERSION])
        return @"0.0";
    return [UD stringForKey:UDVERSION];
}

+ (void)setPreVersion:(NSString *)version {
    [UD setObject:version forKey:UDVERSION];
    [UD synchronize];
}

@end
